package com.rh.controller;

import com.rh.Enums;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.Clazz;
import com.rh.excrptions.DoException;
import com.rh.service.ClazzService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@CrossOrigin
@Controller
@ResponseBody
/**
 老师和管理员才能访问
 */
@RequiresRoles(value = {"teacher","admin"},logical = Logical.OR )
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    /**
     * 分页
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/getclassRoomPage",method = RequestMethod.GET)
    public Result getclassRoomPage(Integer pageNo, Integer pageSize) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 3;
            }
            Page clazzPage = clazzService.getClazzPage(pageNo, pageSize);
            Result result = new Result();
            result.setData(clazzPage);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
    /**
     * 添加班级
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/addClassRoom",method = RequestMethod.POST)
    public Result addClassRoom(Clazz clazz){
        try{
            //int i=1/0;
            clazzService.addMethod(clazz);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.ADD_ERROR);
        }
    }
    /**
     * 删除班级
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/deleteClass",method = RequestMethod.GET)
    public Result deleteClass(Integer classId){
        try{
            //int i=1/0;
            clazzService.deleteMethod(classId);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.DELETE_ERROR);
        }
    }
    /**
     * 更新班级
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/updateClassRoom",method = RequestMethod.POST)
    public Result updateClassRoom(Clazz clazz){
        try{
            //int i=1/0;
            clazzService.updateMethod(clazz);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.UPDATE_ERROR);
        }
    }
    /**
     * 通过id查询班级信息
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/getClassRoomByClassId",method = RequestMethod.GET)
    public Result getClassRoomByClassId(Integer classId){
       try {
           Clazz methodAllById = clazzService.getMethodAllById(classId);
           Result result = new Result();
           result.setData(methodAllById);
           return result;
       }catch (Exception e){
           throw new DoException(Enums.GETLIST_ERROR);
       }
       }
}

